﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Linq;
using Unity;

namespace CustomControls.TreeView
{

    public partial class MyTreeView : UserControl
    {
        public TreeViewNode Contex
        {
            get { return this.DataContext as TreeViewNode; }
        }

        private ObservableCollection<TreeViewNode> TreeViewNodeList = null;//数据源
        FindVisualChild findVisual = new FindVisualChild();

        public delegate void CallBDeleg(TEventArgs<TreeViewNode> args);

        public event CallBDeleg SelectItemEvent;
        public MyTreeView()
        {
            InitializeComponent();
            TreeViewNodeList = new ObservableCollection<TreeViewNode>();
            this.treeView.ItemsSource = TreeViewNodeList;
        }

        /// <summary>
        /// 添加节点数据
        /// </summary>
        /// <param name="node">node节点</param>
        public void AddData(List<TreeViewNode> list)
        {
            TreeViewNodeList.AddRange(GetNodes(0, list));
        }

        private List<TreeViewNode> GetNodes(int parentID, List<TreeViewNode> nodes)
        {
            List<TreeViewNode> mainNodes = nodes.Where(x => x.ParentId == parentID).ToList();
            List<TreeViewNode> otherNodes = nodes.Where(x => x.ParentId != parentID).ToList();
            foreach (TreeViewNode node in mainNodes)
            {
                node.ChildNodes.AddRange(GetNodes(node.Id, otherNodes));
            }
            return mainNodes;
        }

        /// <summary>
        /// 设置折叠状态
        /// </summary>
        private void TreeView_MouseDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem SelectedfItem = new TreeViewItem();
            if (sender.GetType() == typeof(ToggleButton))
            {
                ToggleButton btn = (ToggleButton)sender;
                System.Windows.Controls.ContentPresenter CP = (System.Windows.Controls.ContentPresenter)btn.Tag;
                SelectedfItem = (TreeViewItem)CP.TemplatedParent;
            }
            else if (sender.GetType() == typeof(Border))
            {
                Border btn = (Border)sender;
                System.Windows.Controls.ContentPresenter CP = (System.Windows.Controls.ContentPresenter)btn.Tag;
                SelectedfItem = (TreeViewItem)CP.TemplatedParent;
            }
            else if (e.Source.GetType() == typeof(TreeViewItem))
            {
                SelectedfItem = (TreeViewItem)sender;
            }
            if (SelectedfItem == null || ((TreeViewNode)SelectedfItem.DataContext).IsAllowClick)
            {
                if (SelectItemEvent != null)
                {
                    DefaultTreeview();
                    SelectItemEvent(new TEventArgs<TreeViewNode>((TreeViewNode)SelectedfItem.DataContext));
                    TreeViewItem treeView = VisualHelper.FindParent<TreeViewItem>(SelectedfItem);
                    if (treeView != null)
                    {
                        TreeViewNode viewNode = treeView.DataContext as TreeViewNode;
                        viewNode.RectangleBd = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2878ca"));
                        var url = viewNode.IconUrl;
                        viewNode.IconUrl = viewNode.SelectIconUrl;
                        viewNode.IsChange = true;
                        viewNode.ShowTextBlockForeGroud= new SolidColorBrush((Color)ColorConverter.ConvertFromString("#e6f6ff"));
                        viewNode.SelectIconUrl = url;
                    }
                }
            }
            else 
            {
                findVisual.CloseAllTreeItemExtend(this.treeView);
                SelectedfItem.IsExpanded = SelectedfItem.IsExpanded == true ? false : true;
            }
        }

        private void DefaultTreeview() 
        {
            foreach (TreeViewNode item in treeView.Items) 
            {
                if (item.IsChange)
                {
                    item.RectangleBd = new SolidColorBrush(Colors.Transparent);
                    var url = item.IconUrl;
                    item.IconUrl = item.SelectIconUrl;
                    item.SelectIconUrl = url;
                    item.ShowTextBlockForeGroud = Application.Current.FindResource("ForegroundGloabal") as SolidColorBrush;
                    item.IsChange = false;
                }
            }
        }

    }
}
